package com.wugenqiang.project.system.controller;

import com.wugenqiang.constant.ParamConstant;
import com.wugenqiang.project.system.pojo.User;
import com.wugenqiang.project.system.service.IUserService;

import com.wugenqiang.utils.respond.R;
import com.wugenqiang.utils.verify.RandomValidateCodeUtil;
import org.apache.commons.lang3.StringUtils;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;


import javax.annotation.Resource;


import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;

/**
 * @Classname LoginController
 * @Description 后台登录/登出
 * @Date 2019/1/24 20:19
 * @Created by WuGenQiang//fighting!!
 */
@Controller
public class LoginController extends BaseController{

    private static final Logger LOGGER = LoggerFactory.getLogger(LoginController.class);

    @Resource
    private IUserService usersService;

    @GetMapping(value = "/lfd/admin/login")
    public String login() {
        return "login";
    }


    @PostMapping("/lfd/admin/login")
    @ResponseBody
    public R doLogin(@RequestParam String username,@RequestParam String password,@RequestParam String verify, HttpServletRequest request) {

        try {
            User user = usersService.login(username, password);
            request.getSession().setAttribute(ParamConstant.LOGIN_SESSION_KEY, user);
            if(user==null){
                return R.error("用户名或密码输入错误");}
            //从session中获取随机数
            String random = (String) request.getSession().getAttribute(RandomValidateCodeUtil.RANDOMCODEKEY);
            if (StringUtils.isBlank(verify)) {
                return R.error("请输入验证码");
            }
            if (random.equals(verify)) {
            } else {
                return R.error("请输入正确的验证码");
            }
        } catch (Exception e) {
            LOGGER.error("验证码校验失败", e);
            return R.error("验证码校验失败");
        }
        return R.ok();
    }

    /**
     * 生成验证码
     */
    @GetMapping(value = "/lfd/verify/getVerify")
    public void getVerify(HttpServletRequest request, HttpServletResponse response) {
        try {
            response.setContentType("image/jpeg");//设置相应类型,告诉浏览器输出的内容为图片
            response.setHeader("Pragma", "No-cache");//设置响应头信息，告诉浏览器不要缓存此内容
            response.setHeader("Cache-Control", "no-cache");
            response.setDateHeader("Expire", 0);
            RandomValidateCodeUtil randomValidateCode = new RandomValidateCodeUtil();
            randomValidateCode.getRandcode(request, response);//输出验证码图片方法
        } catch (Exception e) {
            LOGGER.error("获取验证码失败>>>> ", e);
        }
    }

    /**
     * 注销
     *
     * @param session
     * @param response
     */
    @RequestMapping("/lfd/admin/logout")
    public void logout(HttpSession session, HttpServletResponse response) {
        session.removeAttribute(ParamConstant.LOGIN_SESSION_KEY);
        Cookie cookie = new Cookie(ParamConstant.USER_IN_COOKIE, "");
        cookie.setValue(null);
        cookie.setMaxAge(0);// 立即销毁cookie
        cookie.setPath("/");
        response.addCookie(cookie);
        try {
            response.sendRedirect("/lfd/admin/login");
        } catch (IOException e) {
            e.printStackTrace();
            LOGGER.error("注销失败", e);
        }
    }
}
